package com.zxjbyte.yiyi.server.error;

import cn.hutool.core.util.StrUtil;
import com.zxjbyte.yiyi.framework.common.constant.Constant;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * @Author zhangxingjia
 * @Date 2025/3/7 15:03
 * @Version: 1.0
 */
@Component
public class RestErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
        String originalPath = (String) webRequest.getAttribute("javax.servlet.error.request_uri", WebRequest.SCOPE_REQUEST);
        errorAttributes.put("path", originalPath);
        String prefix = StrUtil.emptyToDefault(webRequest.getContextPath(), "") + Constant.BASE_PATH_EXTENSION;
        if(originalPath != null && originalPath.matches("^" + prefix +"(/.*|$)")){
            errorAttributes.put("description", "Extension扩展组件已停用，请配置开启后再使用！如有疑问，请联系作者（微信：zhangxingjia2008）。");
        }
        return errorAttributes;
    }
}
